﻿using MySqlConnector;
using Practice_DotNet.EFCore;
using Practice_DotNet.Helper;
using Practice_DotNet.Model;
using Practice_DotNet.Operation;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
namespace ConnectMySQLPractice
{
    internal class Program
    {
        static async Task Main(string[] args)
        {

            #region 数据库连接和增删改查练习
            /*
            using (var connection = new MySqlConnection(connectionString))
            {
                Console.WriteLine("正在连接数据库...");
                await connection.OpenAsync();
                Console.WriteLine("数据库连接成功！");
                using (var command = new MySqlCommand(commandStr, connection))
                {
                    #region 使用reader读取数据
                    //using var reader = command.ExecuteReader();
                    //while (reader.Read())
                    //{
                    //    string[] fieldValues = new string[reader.FieldCount];
                    //    for (int i = 0; i < reader.FieldCount; i++)
                    //    {
                    //        string value = (reader.GetValue(i) ?? "").ToString();
                    //        fieldValues[i] = CenterString(value, 20);
                    //        //line += value.PadRight(10); // 每列宽20，左对齐
                    //    }
                    //    sqlResult.Add(string.Join('|', fieldValues));
                    //}
                    #endregion

                    #region 使用datatable储存数据
                    //MySqlDataAdapter adapter = new MySqlDataAdapter(command);
                    //DataSet ds = new DataSet();
                    //adapter.Fill(ds);
                    //var dt = ds.Tables[0];
                    //Console.WriteLine(dt.Rows.Count);
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //{
                    //    Console.WriteLine($"{dt.Rows[i]["email"].ToString().PadRight(10)}|{dt.Rows[i]["username"].ToString().PadRight(10)}|{dt.Rows[i]["createdat"].ToString().PadRight(10)}");
                    //}            
                    #endregion
                }

            }*/
            #endregion

            #region 控制台查询系统模拟练习
            ////开始登陆
            //Console.WriteLine("欢迎登陆XX系统");
            ////选择语言
            //Console.WriteLine("请选择语言：\r\n1.中文\r\n2.English");
            //var language = Console.ReadLine() ?? "";

            ////根据选择设置语言
            //InfoHelper.SelectLanguage(language);

            using var context = new MyDbContext();
            
            
            List<UserInfoModelForEFCore> userInfos = context.userInfosDbSet.ToList();
            Console.WriteLine($"查询到{userInfos.Count}条数据");
            for (int i = 0; i < userInfos.Count; i++)
            {
                Console.WriteLine($"{userInfos[i].ID},{userInfos[i].Account},{userInfos[i].Password},{userInfos[i].PetName},{userInfos[i].Gender}");
            }
            /*
            //判断登陆信息是否正确,如果不正确则重复提示登陆
            while (true)
            {
                Console.WriteLine(InfoHelper.AccountInputText);
                string account = Console.ReadLine() ?? "";
                Console.WriteLine(InfoHelper.PasswordInputText);
                string passWord = TextHelper.ReadPassword();
                Console.WriteLine();
                UserInfoModel? userInfo = await UserInfoOperation.GetUserInfo(account, passWord);
                if (userInfo != null)
                {
                    //如果性别为空则提示录入性别信息
                    if (userInfo.Gender != 1 && userInfo.Gender != 2)
                    {
                        //如果选择的性别信息不对则重新选择，选对了就更新到数据库中
                        while (true)
                        {
                            Console.WriteLine(InfoHelper.GenderInputText);
                            var inputGender = Console.ReadLine();
                            if (inputGender == "1")
                            {
                                try
                                {
                                    userInfo.Gender = 1;
                                    int effectLine = await UserInfoOperation.UpdateUserInfo(userInfo);
                                    if (effectLine > 0)
                                    {
                                        Console.WriteLine(InfoHelper.GenderAddSuccessText);
                                    }
                                    else if (effectLine == 0)
                                    {
                                        Console.WriteLine(InfoHelper.UpdateInfoErrorText);
                                    }
                                    break;
                                }
                                catch (DbException ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                            else if (inputGender == "2")
                            {
                                try
                                {
                                    userInfo.Gender = 2;
                                    int effectLine = await UserInfoOperation.UpdateUserInfo(userInfo);
                                    if (effectLine > 0)
                                    {
                                        Console.WriteLine(InfoHelper.GenderAddSuccessText);
                                    }
                                    break;
                                }
                                catch (DbException ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                            }
                            else
                            {
                                Console.WriteLine(InfoHelper.GenderAddFailedText);
                                continue;
                            }

                        }

                    }
                    string userGenderText = "";
                    if (userInfo.Gender == 1)
                    {
                        userGenderText = InfoHelper.Gender1Text;
                    }
                    if (userInfo.Gender == 2)
                    {
                        userGenderText = InfoHelper.Gender2Text;
                    }

                    Console.WriteLine(InfoHelper.CurrentUserInfoText);
                    PrintUserInfo(userInfo);
                    break;
                }
                else
                {
                    Console.WriteLine(InfoHelper.AccountOrPwdErrorText);
                }
            }
            //打印所有用户信息
            List<UserInfoModel> userInfos = await UserInfoOperation.GetAllUserInfo();
            Console.WriteLine();
            if (userInfos.Count > 0)
            {
                Console.WriteLine(InfoHelper.AllUserInfoText);
                Console.WriteLine("---------------------------------------------------------------");
                foreach (UserInfoModel userInfo in userInfos)
                {
                    List<LeftJoinModel>? userScores = await LeftJoinOperation.GetAllData();
                    if (userScores == null || userScores.Count == 0)
                    {
                        PrintUserInfo(userInfo);
                        Console.WriteLine("---------------------------------------------------------------");
                        continue;
                    }
                    foreach (var userScore in userScores)
                    {
                        PrintUserInfo(userInfo, userScore);
                        Console.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
                    }
                    Console.WriteLine("---------------------------------------------------------------");
                    var scoreResult = await LeftJoinOperation.GetAllData();
                    var singScoreResult = scoreResult.OrderBy(s => s.RecordDate).First();
                    Console.WriteLine(InfoHelper.LastScoreText);
                    PrintUserInfo(userInfo, singScoreResult);
                    Console.WriteLine("---------------------------------------------------------------");

                }
            }*/
            #endregion
        }
        private static void PrintUserInfo(UserInfoModel userInfo)
        {
            string genderText = userInfo.Gender == 1 ? InfoHelper.Gender1Text : InfoHelper.Gender2Text;
            Console.WriteLine($"""
            {InfoHelper.AccountText}{userInfo.Account}
            {InfoHelper.PasswordText}{userInfo.Password}
            {InfoHelper.PetNameText}{userInfo.PetName}
            {InfoHelper.GenderText}{genderText}
            """);
        }
        private static void PrintUserInfo(UserInfoModel userInfo, LeftJoinModel userScores)
        {
            string genderText = userInfo.Gender == 1 ? InfoHelper.Gender1Text : InfoHelper.Gender2Text;
            Console.WriteLine($"""
            {InfoHelper.AccountText}{userInfo.Account}
            {InfoHelper.PasswordText}{userInfo.Password}
            {InfoHelper.PetNameText}{userInfo.PetName}
            {InfoHelper.GenderText}{genderText}
            {userScores.Chinese}
            {userScores.English}
            {userScores.Math}
            {userScores.RecordDate.ToString("yyyy-MM-dd")}
            """);
        }
    }
}